package com.ztrue.common.utilities;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangw on 2016/4/5.
 * 类操作
 */
public class ClassUtil {
    /**
     * 获取类的字段
     *
     * @param cls    类
     * @param endCls 结束循环父类（可默认空）
     * @return
     */
    public static List<Field> getFields(Class cls, Class endCls) {
        List<Field> fieldList = new ArrayList<Field>();
        Field baseFields[] = cls.getDeclaredFields();//获得对象所有属性
        int l = baseFields.length;
        for (int i = 0; i < l; i++) {
            fieldList.add(baseFields[i]);
        }

        if (endCls != null && !cls.equals(endCls)) {
            fieldList.addAll(getFields(cls.getSuperclass(), endCls));
        }
        return fieldList;
    }
}
